package com.example.m_kartupay;

import android.net.ParseException;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
import android.widget.AdapterView.OnItemClickListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.m_kartupay.R;
import com.example.m_kartupay.LoginActivity.LoginLogOut;
import com.example.m_kartupay.library.DatabaseHandler;
import com.example.m_kartupay.library.JSONParser;
import com.example.m_kartupay.library.StrictWrapper;
import com.example.m_kartupay.library.UserFunctions;
import com.example.m_kartupay.constuctor.*;
import com.example.m_kartupay.AccountTransactions;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;



public class AccountTransactions extends Activity {
	private JSONParser jsonParser;
	private static String url_transaction = "http://api.kartupay.com/login";
	private ProgressDialog pDialog;	
	JSONParser jParser = new JSONParser();
	private ArrayList<HashMap<String, String>> myTransaction;
	private static final String TAG_INVOICE = "invoice";
	private static final String TAG_ORDERNO = "order_no";
	
	ArrayList<TransactionHistory> transactionList;
	
	TransactionHistoryAdapter adapter;
	
	 @Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.account_transactions);
	    StrictWrapper.init();
	    transactionList = new ArrayList<TransactionHistory>();
	    
	    new JSONAsyncTask().execute("http://api.kartupay.com/i/transaction-history");
		
		ListView listview = (ListView)findViewById(R.id.list);
		adapter = new TransactionHistoryAdapter(getApplicationContext(), R.layout.list_transactions, transactionList);
		
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), transactionList.get(position).getInvoice(), Toast.LENGTH_LONG).show();				
			}
		});

	  	        
	 }
	 class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
			
			ProgressDialog dialog;
			
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				dialog = new ProgressDialog(AccountTransactions.this);
				dialog.setMessage("Loading, please wait");
				dialog.setTitle("Connecting server");
				dialog.show();
				dialog.setCancelable(false);
			}
			
			@Override
			protected Boolean doInBackground(String... urls) {
				DatabaseHandler db = new DatabaseHandler(getApplicationContext());
				HashMap<String, String> account = db.getUserDetails();		
				String channel_id = "123456789";
				String user_id = (String)account.get("user_id");
				String email = (String)account.get("email");
				String limit = "2";
				String page = "1";
				Log.d("channel_id",channel_id);
				Log.d("user_id",user_id);
				Log.d("email",email);
				Log.d("limit",limit);
				Log.d("page",page);
				UserFunctions userFunction = new UserFunctions();
				JSONObject json = userFunction.transactionHistory(urls[0],channel_id, user_id, "ahalomoan@gmail.com", limit, page);
							
				try {	
					String res = json.getString("responseCode");
					Log.d("All Transaction: ", json.toString());
					Log.d("ResponseCode",res);
					if(Integer.parseInt(res) ==00){			
						
							JSONObject obj_data = json.getJSONObject("data");	
												
							JSONArray result = obj_data.getJSONArray("results"); 		
							
							for (int i = 0; i < result.length(); i++) { 					
				            
				                Log.d("Array Data Index",result.toString(i));
				                //JSONObject json_data = result.getJSONObject(i);
				                
				                JSONObject c = result.getJSONObject(i);
				                Log.d("Invoice",c.getString("invoice"));
				                TransactionHistory transaction = new TransactionHistory();
				                
				                String invoice = c.getString(TAG_INVOICE);
				                String order_no = c.getString(TAG_ORDERNO);
				                transaction.setInvoice(invoice);
				                transaction.setOrderNo(order_no);
				                
				                transactionList.add(transaction);			               
												                
							}
							return true;
					}
				} catch (ParseException e1) {
					e1.printStackTrace();
				}  catch (JSONException e) {
					e.printStackTrace();
				}
				return false;
			}
			
			protected void onPostExecute(Boolean result) {
				dialog.cancel();
				adapter.notifyDataSetChanged();
				if(result == false)
					Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

			}

	 }
	}


